# -*- coding: utf-8 -*-

import mysql.connector
import MySQLdb

import traceback
from py27_db import db_execute, db_fetchone, db_fetchall


def mysql_connect(module=MySQLdb, host="127.0.0.1", port=3306, user="root", passwd="chench", db="mysql"):
    """"连接数据库MySQL"""
    try:
#        config = {
#            'host': 'localhost',
#            'user': 'root',
#            'password': 'chench',
#            'port': 3306,
#            'database': 'mysql',
#            'charset': 'utf8'
#        }
#        connect = module.connect(**config)
        connect = MySQLdb.connect(
            host=host,
            port=port,
            user=user,
            passwd=passwd,
            db=db)
        return connect
    except Exception as e:
        print("error:", traceback.format_exc(e))
        return None
    pass



if __name__ == '__main__':
    
    
    connect = mysql_connect(module=mysql.connector, host='127.0.0.1', port=3306, user="root", passwd="chench", db="mysql")
    
    
    sql_str = 'select version();'
    cursor = db_execute(connect=connect, sql_str=sql_str)
    result = db_fetchone(cursor=cursor)
    print('result:', result)
   
    sql_str = 'select `host`, `user` from `user`;'
    cursor = db_execute(connect=connect, sql_str=sql_str)
    result = db_fetchall(cursor=cursor)
    print('result:', result)

    pass